package com.interactiveyang.meet;

import oauth.signpost.OAuth;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity implements OnSharedPreferenceChangeListener, OnClickListener, OnFocusChangeListener{
	
	LinearLayout linearLayout;
	TextView tv;
	Button latitudeButton;
	Button oauthButton;
	EditText username;
	EditText password;
	static CommonsHttpOAuthConsumer consumer;
	static CommonsHttpOAuthProvider provider;
	
	static public SharedPreferences prefs;
	public Editor prefEditor;
	private String token, secret;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		C.setFullScreen(this);
		this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.main);
        
        tv = (TextView) findViewById(R.id.TextView01);
        oauthButton  = (Button) findViewById(R.id.OauthButton);
        latitudeButton = (Button)findViewById(R.id.LoginButton);
        
        username = (EditText) findViewById(R.id.Username);
        password = (EditText) findViewById(R.id.Password);
        
        //	Get the preference file.
        prefs = getSharedPreferences(C.OAuth.PREF_NAME, MODE_PRIVATE);
		prefs.registerOnSharedPreferenceChangeListener(this);
		prefEditor = prefs.edit();
        
		token = prefs.getString(OAuth.OAUTH_TOKEN, "");
		secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
		
		tv.setText(token);
		
		if(secret != null && token != null){
			oauthButton.setVisibility(View.INVISIBLE);
		}
		
		oauthButton.setOnClickListener(this);
        latitudeButton.setOnClickListener(this);
        username.setOnClickListener(this);
        password.setOnClickListener(this);
    }
    
    public void onClick(View v){
    	if(v == oauthButton){
    		startActivity(new Intent().setClass(v.getContext(), OAuthMain.class));
    	}
    	else if(v == latitudeButton){
    		
			// TODO check the sharedpreference to auto login
    		if(username.getText() != null && password.getText() != null){
    			String result = "";
    			
    			String usr = username.getText().toString();
    			String pwd = password.getText().toString();
    			
    			if(C.MeetBack.backendRequest.checkUserLogin(usr, pwd)){
    				Toast t = Toast.makeText(this, "login successed!!!", Toast.LENGTH_LONG);
    				
    				prefEditor.putString(C.UserInfo.MEET_USERNAME_PREFTITLE, usr);
    				prefEditor.putString(C.UserInfo.MEET_PASSWORD_PREFTITLE, pwd);
    				prefEditor.commit();
    				
    				startActivity(new Intent().setClass(v.getContext(), MapPage.class));
    				t.show();
    			}
    			else{
    				Toast t = Toast.makeText(this, "login failed!!!", Toast.LENGTH_LONG);
    				t.show();
    			}
    			
    		}
    		
    	}
    	else if(v == username || v == password){
    		((EditText) v).setText("");
    	}
    }
    
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		String value = sharedPreferences.getString(key, "");

		if (key.equals(OAuth.OAUTH_TOKEN))
			token = value;
		else if (key.equals(OAuth.OAUTH_TOKEN_SECRET)) secret = value;

		Log.i("GDATA MAIN", key + " = " + value);
		
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
	}
	
	
}